package com.aidong.dao.base;

import java.util.ArrayList;
import java.util.Iterator;
import java.util.List;

public class ConditionMap {
    public static final int AND = 0;
    public static final int OR = 1;
    private String andOr;
    private List<Condition> cdList = new ArrayList();

    public ConditionMap() {
        this.andOr = " and ";
    }

    public ConditionMap(int addType) {
        this.andOr = addType == 0?" and ":"  or ";
    }

    public ConditionMap eq(String name, Object value) {
        return this.add(name, "=", value);
    }

    public ConditionMap ne(String name, Object value) {
        return this.add(name, "<>", value);
    }

    public ConditionMap gt(String name, Object value) {
        return this.add(name, ">", value);
    }

    public ConditionMap ge(String name, Object value) {
        return this.add(name, ">=", value);
    }

    public ConditionMap lt(String name, Object value) {
        return this.add(name, "<", value);
    }

    public ConditionMap le(String name, Object value) {
        return this.add(name, "<=", value);
    }

    public ConditionMap like(String name, Object value) {
        return this.add(name, "like", value);
    }

    public ConditionMap notLike(String name, Object value) {
        return this.add(name, "not like", value);
    }

    public ConditionMap between(String name, Object value1, Object value2) {
        return this.add(name, "between", new Object[]{value1, value2});
    }

    public ConditionMap isNull(String name) {
        return this.add(name, "is null", (Object)null);
    }

    public ConditionMap isNotNull(String name) {
        return this.add(name, "is not null", (Object)null);
    }

    public ConditionMap in(String name, Object... value) {
        return this.add(name, "in", value);
    }

    public ConditionMap notIn(String name, Object... value) {
        return this.add(name, "not in", value);
    }

    public ConditionMap addSqlw(String sqlw, Object... value) {
        return this.add(sqlw, "sqlw", value);
    }

    public ConditionMap addSub(ConditionMap cd) {
        return this.add("", "condition", cd);
    }

    private ConditionMap add(String name, String operate, Object value) {
        if(name != null) {
            this.cdList.add(new Condition(name, operate, value));
        }

        return this;
    }

    private ConditionMap clear() {
        this.cdList.clear();
        return this;
    }

    public String toSql(List<Object> params) {
        StringBuilder sql = new StringBuilder();
        Iterator i$ = this.cdList.iterator();

        while(true) {
            String ss;
            Condition cd;
            Object[] p;
            int i;
            do {
                label87:
                do {
                    while(i$.hasNext()) {
                        cd = (Condition)i$.next();
                        if("sqlw".equals(cd.getOperate())) {
                            ss = cd.getName();
                            continue label87;
                        }

                        if(!"is null".equals(cd.getOperate()) && !"is not null".equals(cd.getOperate())) {
                            if(cd.getValue() != null && (!(cd.getValue() instanceof String) || ((String)cd.getValue()).length() != 0)) {
                                if("condition".equals(cd.getOperate())) {
                                    ss = ((ConditionMap)cd.getValue()).toSql(params);
                                    if(ss != null && ss.length() > 0) {
                                        sql.append(this.andOr).append("(" + ss + ")");
                                    }
                                } else if(!"in".equals(cd.getOperate()) && !"not in".equals(cd.getOperate())) {
                                    if("between".equals(cd.getOperate())) {
                                        p = (Object[])((Object[])cd.getValue());
                                        if(p.length == 2 && p[0] != null && p[1] != null) {
                                            sql.append(this.andOr).append(cd.getName() + " " + cd.getOperate() + " :p" + (params.size() + 1) + " and :p" + (params.size() + 2));
                                            params.add(p[0]);
                                            params.add(p[1]);
                                        }
                                    } else {
                                        sql.append(this.andOr).append(cd.getName() + " " + cd.getOperate() + " :p" + (params.size() + 1));
                                        params.add(cd.getValue());
                                    }
                                } else {
                                    p = (Object[])((Object[])cd.getValue());
                                    if(p.length > 0) {
                                        sql.append(this.andOr).append(cd.getName() + " " + cd.getOperate() + " (");

                                        for(i = 0; i < p.length; ++i) {
                                            if(i > 0) {
                                                sql.append(", ");
                                            }

                                            sql.append(":p" + (params.size() + 1));
                                            params.add(p[i]);
                                        }

                                        sql.append(")");
                                    }
                                }
                            }
                        } else {
                            sql.append(this.andOr).append(cd.getName() + " " + cd.getOperate());
                        }
                    }

                    if(sql.length() > 5) {
                        sql.delete(0, 5);
                    }

                    return sql.toString();
                } while(ss == null);
            } while(ss.length() <= 0);

            p = (Object[])((Object[])cd.getValue());
            if(p.length > 0) {
                for(i = 0; i < p.length; ++i) {
                    ss = ss.replace("?", ":p" + (params.size() + 1));
                    params.add(p[i]);
                }
            }

            sql.append(this.andOr).append(ss);
        }
    }
}
